//
//  FollowNumberHistoryViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddFollowNumberHistoryViewController.h"
#import "JddCommonFunction.h"
#import "JddViewControllerManager.h"
#import "JddDataParser.h"
#import "SBJson.h"

@interface JddFollowNumberHistoryViewController ()

@end

@implementation JddFollowNumberHistoryViewController

@synthesize cellValue;
@synthesize charseID;
@synthesize dataSourID;
@synthesize listTable;
@synthesize count;
@synthesize next;
@synthesize prev;
@synthesize tipLable;


SHARED_VIEW_CONTROLLER_IMPLEMENT(JddFollowNumberHistoryViewController);

-(IBAction)selectNext:(id)sender{
    if(hasNext){
        pageNo ++;
        [self startQueryOperation];
    }
}

-(IBAction)selectPrev:(id)sender{
    if (hasPrev) {
        pageNo --;
        [self startQueryOperation];
    }
    
}

-(void)followNumberButtonClicked:(UIButton *)button
{
//    charseID = chaseId;/
    NSDictionary *cellindex = [cellValue objectAtIndex:button.tag - 1000];
    charseID = [cellindex objectForKey:@"ID"];
    NSLog(@"点击后chaseId = %@",charseID);
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_FOLLOW_NUMBER_DETAIL
                                                            fromController:self
                                                                withObject:nil
                                                               withObject2:charseID];

}

- (void)QueryFollowNumberOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];

    if (result) {

        if ([JddDataParser parseModifyPasswordResult:result]) {
            SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
            NSDictionary *dictResult = [jsonParser objectWithData:result];
            NSMutableDictionary *data = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            NSMutableArray *sportListString = [data objectForKey:@"item"];
            
            if (pageNo > 1) {
                for (NSMutableArray *sport in sportListString) {
                    [cellValue addObject:sport];
                }
                
            } else {
                cellValue = sportListString;
                if ([cellValue count]>0) {
                    tipLable.hidden = YES;
                    next.hidden = YES;

                }else {
                    tipLable.hidden = NO;
                }
            }
            if ([[data objectForKey:@"Count"] intValue] == [cellValue count]) {
                next.hidden = YES;
                hasNext = NO;
            }else {
                next.hidden = NO;
                hasNext = YES;
            }
            
            [listTable reloadData];
        } else {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [cellValue count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JddFollowNumberItemCell *cell = [JddFollowNumberItemCell getInstanceView];
    NSDictionary *cellindex = [cellValue objectAtIndex:indexPath.row];
    cell.MoneyLabel.text =[NSString stringWithFormat:@"%d彩豆",[[cellindex objectForKey:@"Money"] intValue]];
    cell.LotNameLabel.text = [cellindex objectForKey:@"LotName"];
    cell.IssueNumLabel.text = [NSString stringWithFormat:@"%d期",[[cellindex objectForKey:@"IssueNum"] intValue]];
    cell.buyedIssueNumLabel.text = [NSString stringWithFormat:@"%d期",[[cellindex objectForKey:@"BuyedIssueNum"] intValue]];
    cell.stateLabel.text = [cellindex objectForKey:@"State"];
    cell.charseID = [cellindex objectForKey:@"ID"];
    cell.datasID = [cellindex objectForKey:@"DataSource"];
    cell.DateLabel.text = [cellindex objectForKey:@"DateTime"];
    cell.code = @"表单";
    
    cell.bgButton.tag = 1000 + indexPath.row;
    [cell.bgButton addTarget:self action:@selector(followNumberButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85.0;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        
//    [self.navigationItem setTitle:PAGE_FOLLOW_NUMBER_HISTORY];
   
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_FOLLOW_NUMBER_HISTORY];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;

//    if ( [self.fromController class] == [JddLoginViewController class]) {
//        [self.navigationItem setHidesBackButton:YES];
//    }
    
    cellValue = [[NSMutableArray alloc] init];
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    listTable.tableFooterView = v;
    [listTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [listTable setSeparatorInset:UIEdgeInsetsZero];
    }
    
}

-(void)destoryView{
    if ([cellValue count]>0) {
        [cellValue removeAllObjects];
    }
    cellValue = [[NSMutableArray alloc] init];
    [listTable reloadData];
}

-(void)startQueryOperation{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryFollowNumberOperation *operation = [[JddQueryFollowNumberOperation alloc] init];
    operation.delegate = self;
    operation.pageNo =[NSString stringWithFormat:@"%d", pageNo];
    operation.pageSize= @"10";
    operation.lotID = @"0"; 
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"FollowNumber"];
    [MobClick event:@"UserCenter-zhuihaojilu"];

//    prev.hidden = YES;
    next.hidden = YES;
    tipLable.hidden = YES;
    
    count = 0;
    pageNo = 1;
    [self startQueryOperation];
    [listTable reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"FollowNumber"];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



@end
